package com.fss.daoTestCases;

import java.util.*;

import junit.framework.TestCase;

import com.fss.user.dao.UserDaoImpl;
import com.fss.secondHand.dao.*;
import com.fssDataModels.*;

public class TestSecondHandDaoImpl extends TestCase{
	public TestSecondHandDaoImpl(
	        String name) {
	        super(name);
	    }
	
	private SecondHandDaoImpl db2 = new SecondHandDaoImpl();//connection to database
	private UserDaoImpl db = new UserDaoImpl();//connection to database
	
		/*========test right-data cases:============================*/		 
	    public void testInsert() {
	    	TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	       
	        db.insertUser(newUser);
	    	
	    	TbUser author = db.getUser("user2");
	    	
	    	TbSecondhandItem newItem = new TbSecondhandItem(author,"special second hand ","dummy content",new Date());
	        
	        db2.insertSecondHandItem(newItem);
	        
	        
	        List items = db2.getSecondHandItems(author);
	        TbSecondhandItem storedItem = (TbSecondhandItem)items.get(0);
	        	        	    	   
	        assertEquals("special second hand ",storedItem.getTitle());
	        
	        //clean up
	        db2.deleteSecondHandItem(storedItem);
	        db.deleteUser(newUser);
	    }
	    
	    public void testUpdate() {
	    	TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
		       
	        db.insertUser(newUser);
	    	
	    	TbUser author = db.getUser("user2");
	    	
	    	TbSecondhandItem newItem = new TbSecondhandItem(author,"special second hand ","dummy content",new Date());
	        
	        db2.insertSecondHandItem(newItem);
	        
	        
	        List items = db2.getSecondHandItems(author);
	        TbSecondhandItem storedItem = (TbSecondhandItem)items.get(0);
	        
	        storedItem.setTitle("new title");
	        db2.updateSecondHandItem(storedItem);
	        TbSecondhandItem updatedItem = (TbSecondhandItem)items.get(0);
	        
	        	        	    	   
	        assertEquals("new title",updatedItem.getTitle());
	        	        
     	    //clean up
	        db2.deleteSecondHandItem(storedItem);
	        db.deleteUser(newUser);
	    }	    	   	    
	    /*============test wrong-data cases:==========================*/
	    
	    
	    /*=================entry point of the testing program===========================*/
	    public static void main(String[] args) {
	        junit.textui.TestRunner.run(
	        		TestSecondHandDaoImpl.class);
	    }
}
